use axum::Json;
use serde::Serialize;
#[derive(Serialize)]
pub struct R<'a, T> {
    code: u16,
    msg: &'a str,
    data: T,
}
#[derive(Serialize, Debug)]
pub struct List<T> {
    pub data: Vec<T>,
    pub total: i64,
}

pub fn data<T>(data: T, msg: &str) -> Json<R<T>> {
    Json(R { code: 1, msg, data })
}

pub fn err(code: u16, msg: &str) -> Json<R<String>> {
    Json(R {
        code,
        msg,
        data: "".to_string(),
    })
}

pub fn error(error: &str) -> Json<R<String>> {
    Json(R {
        code: 0,
        msg: error,
        data: "".to_string(),
    })
}
pub fn success(msg: &str) -> Json<R<String>> {
    Json(R {
        code: 1,
        msg,
        data: "".to_string(),
    })
}
pub fn info<'a>(result: bool, ok_text: &'a str, err_text: &'a str) -> Json<R<'a, String>> {
    match result {
        true => success(ok_text),
        false => error(err_text),
    }
}

pub fn list<T>(data: List<T>) -> Json<R<'static, List<T>>> {
    Json(R {
        code: 1,
        msg: "操作成功",
        data,
    })
}
